#!/bin/bash

CURPATH="/iflytek/uoamp/uoampagent"

export LANG=en_US.UTF-8
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${CURPATH}/conf/thirdLibs

. /etc/profile
if [ -e "~/.bash_profile" ];then
	. ~/.bash_profile
elif [ -e "~/.profile" ];then
	. ~/.profile
fi

COMMON=0
MAIN=0
MONITOR=0
LOGGER=0
CORE=0


COMMONPATH="/iflytek/uoamp/uoampagent/lib/uoamp-agent-common.new.jar"
COREPATH="/iflytek/uoamp/uoampagent/lib/uoamp-agent-core.new.jar"
MONITORPATH="/iflytek/uoamp/uoampagent/lib/uoamp-agent-monitor.new.jar"
MAINPATH="/iflytek/uoamp/uoampagent/uoamp-agent-main.new.jar"
LOGGERPATH="/iflytek/uoamp/uoampagent/lib/uoamp-agent-logger.new.jar"
function passAgent(){
	FLAGEID=$(ps aux | grep uoamp-agent-main |grep -v grep|grep -v uoampagent.sh|grep jar| awk '{print $2}')
	if [ "$1" == "start" ];then
		if [ "$FLAGEID" == "" ];then
			exec java -jar -Xms2048m -Xmx2048m -XX:PermSize=128M -XX:MaxPermSize=2048m uoamp-agent-main-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
		fi
		echo "pass start ">> /iflytek/sgy/log/uoamp/agent/redeploy.log
	elif [ "$1" == "stop" ];then
		IDS=$(ps aux | grep "uoamp-agent-main" |grep -v grep | grep -v guardMCAgent | awk '{print $2}' | head -n 1)
        	while [[ "$IDS" != "" ]]
        	do
                	kill -9 "$IDS"
                	IDS=$(ps aux | grep "uoamp-agent-main" |grep -v grep | grep -v guardMCAgent | awk '{print $2}' | head -n 1)
        	done	
		echo "pass stop ">> /iflytek/sgy/log/uoamp/agent/redeploy.log
	elif [ "$1" == "restart" ];then
		IDS=$(ps aux | grep "uoamp-agent-main" |grep -v grep | grep -v guardMCAgent | awk '{print $2}' | head -n 1)
                while [[ "$IDS" != "" ]]
                do
                        kill -9 "$IDS"
                        IDS=$(ps aux | grep "uoamp-agent-main" |grep -v grep | grep -v guardMCAgent | awk '{print $2}' | head -n 1)
                done
		
		exec java -jar -Xms2048m -Xmx2048m -XX:PermSize=128M -XX:MaxPermSize=2048m uoamp-agent-main-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
		echo "pass restart ">> /iflytek/sgy/log/uoamp/agent/redeploy.log
	fi
}


function backjar(){
	echo "back jar">> /iflytek/sgy/log/uoamp/agent/redeploy.log
	#cp -rf MCAgent-0.0.1-SNAPSHOT.jar MCAgent.bak.jar
	#mv -f MCAgent.new.jar MCAgent-0.0.1-SNAPSHOT.jar
  if [ -f "$MAINPATH" ]; then
       echo "uoamp-agent-main.new.jar exiest! " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
       MAIN=1
       cp -rf uoamp-agent-main-0.0.1-SNAPSHOT.jar uoamp-agent-main.bak.jar
	     mv -f uoamp-agent-main.new.jar uoamp-agent-main-0.0.1-SNAPSHOT.jar
	     echo "MAIN = ${MAIN} " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
  fi   

  if [ -f "$COREPATH" ]; then
       echo "uoamp-agent-core.new.jar exiest! " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
       CORE=1
       cp -rf lib/uoamp-agent-core-0.0.1-SNAPSHOT.jar lib/uoamp-agent-core.bak.jar
	     mv -f lib/uoamp-agent-core.new.jar lib/uoamp-agent-core-0.0.1-SNAPSHOT.jar
	     echo "CORE = ${CORE} " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
  fi 	

  if [ -f "$MONITORPATH" ]; then
       echo "uoamp-agent-monitor.new.jar exiest! " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
       MONITOR=1
       cp -rf lib/uoamp-agent-monitor-0.0.1-SNAPSHOT.jar lib/uoamp-agent-monitor.bak.jar
	     mv -f lib/uoamp-agent-monitor.new.jar lib/uoamp-agent-monitor-0.0.1-SNAPSHOT.jar
	     echo "MONITOR = ${MONITOR} " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
  fi 

  if [ -f "$COMMONPATH" ]; then
       echo "uoamp-agent-common.new.jar exiest! " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
       COMMON=1
       cp -rf lib/uoamp-agent-common-0.0.1-SNAPSHOT.jar lib/uoamp-agent-common.bak.jar
	     mv -f lib/uoamp-agent-common.new.jar lib/uoamp-agent-common-0.0.1-SNAPSHOT.jar
	     echo "COMMON = ${COMMON} " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
  fi 

  if [ -f "$LOGGERPATH" ]; then
       echo "uoamp-agent-logger.new.jar exiest! " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
       LOGGER=1
       cp -rf lib/uoamp-agent-logger-0.0.1-SNAPSHOT.jar lib/uoamp-agent-logger.bak.jar
	     mv -f lib/uoamp-agent-logger.new.jar lib/uoamp-agent-logger-0.0.1-SNAPSHOT.jar
	     echo "LOGGER = ${LOGGER} " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
  fi 
      
}

function rebackjar(){
	echo "reback jar" >> /iflytek/sgy/log/uoamp/agent/redeploy.log
	#cp -rf MCAgent-0.0.1-SNAPSHOT.jar MCAgent.new.jar
	#mv -f MCAgent.bak.jar MCAgent-0.0.1-SNAPSHOT.jar

  if [ ${MAIN} -eq 1 ];then
  	 echo "rebackjar uoamp-agent-main-0.0.1-SNAPSHOT.jar " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
  	 cp -rf uoamp-agent-main-0.0.1-SNAPSHOT.jar uoamp-agent-main.new.jar
	   mv -f uoamp-agent-main.bak.jar uoamp-agent-main-0.0.1-SNAPSHOT.jar
  fi

  if [ ${COMMON} -eq 1 ];then
  	 echo "rebackjar uoamp-agent-common-0.0.1-SNAPSHOT.jar " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
	   cp -rf lib/uoamp-agent-common-0.0.1-SNAPSHOT.jar lib/uoamp-agent-common.new.jar
	   mv -f lib/uoamp-agent-common.bak.jar lib/uoamp-agent-common-0.0.1-SNAPSHOT.jar  	 
  fi  

  if [ ${MONITOR} -eq 1 ];then
  	echo "rebackjar uoamp-agent-monitor-0.0.1-SNAPSHOT.jar " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
  	cp -rf lib/uoamp-agent-monitor-0.0.1-SNAPSHOT.jar lib/uoamp-agent-monitor.new.jar
	  mv -f lib/uoamp-agent-monitor.bak.jar lib/uoamp-agent-monitor-0.0.1-SNAPSHOT.jar	 
  fi

  if [ ${LOGGER} -eq 1 ];then
  	echo "rebackjar uoamp-agent-logger-0.0.1-SNAPSHOT.jar " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
  	cp -rf lib/uoamp-agent-logger-0.0.1-SNAPSHOT.jar lib/uoamp-agent-logger.new.jar
	  mv -f lib/uoamp-agent-logger.bak.jar lib/uoamp-agent-logger-0.0.1-SNAPSHOT.jar		 
  fi

  if [ ${CORE} -eq 1 ];then
  	echo "rebackjar uoamp-agent-core-0.0.1-SNAPSHOT.jar " >> /iflytek/sgy/log/uoamp/agent/redeploy.log
  	cp -rf lib/uoamp-agent-core-0.0.1-SNAPSHOT.jar lib/uoamp-agent-core.new.jar
	  mv -f lib/uoamp-agent-core.bak.jar lib/uoamp-agent-core-0.0.1-SNAPSHOT.jar	 
  fi    

}

function vaildateStatus(){
	isstart=$(ps aux | grep uoamp-agent-main |grep -v grep|grep -v uoampagent.sh|grep jar| awk '{print $2}')
	if [ "$isstart" == "" ];then
		echo "run reback" >> /iflytek/sgy/log/uoamp/agent/redeploy.log
		passAgent stop
		echo "stop ok" >> /iflytek/sgy/log/uoamp/agent/redeploy.log
		rebackjar
		passAgent start
	fi
}

function self-starting(){    
    if [ -x "/etc/rc.local" ];
    then
        echo "continue....." >> /iflytek/sgy/log/uoamp/agent/redeploy.log
    else
        chmod +x /etc/rc.local
    fi

    if cat /etc/rc.local | grep './uoampagent.sh start' > /dev/null;
    then
        echo shell exists!
    elif cat /etc/rc.local | grep 'exit 0' | grep -v "#" > /dev/null;
        then
            sed -i 's/exit 0/cd \/iflytek\/uoamp\/uoampagent\//g' /etc/rc.local
            echo "./uoampagent.sh start" >> /etc/rc.local
            echo "exit 0" >> /etc/rc.local
            echo "add exit 0 to rc.load success!" >> /iflytek/sgy/log/uoamp/agent/redeploy.log
        else
            echo "cd /iflytek/uoamp/uoampagent/" >> /etc/rc.local
            echo "./uoampagent.sh start" >> /etc/rc.local
            echo "add to rc.local success!" >> /iflytek/sgy/log/uoamp/agent/redeploy.log
    fi
}

echo "start redeploy "> /iflytek/sgy/log/uoamp/agent/redeploy.log
passAgent stop
backjar
passAgent start
sleep 5
vaildateStatus
self-starting